package dao;

import bean.Express;
import bean.Guarantee;
import util.DriverManegerUitl;

import java.sql.*;
import java.util.ArrayList;

public class GuaranteeDao {
    private Connection conn;
    private PreparedStatement pre;
    private ResultSet resultSet;

    /**
     * @return
     */

    public ArrayList<Guarantee> getGuaranteesByDormitoryid2() {
        ArrayList<Guarantee> guarantees = new ArrayList<>();
        conn = DriverManegerUitl.getConnection();
        try {
            pre = conn.prepareStatement("select *from guarantee");
            resultSet = pre.executeQuery();
            while (resultSet.next()) {

                Guarantee guarantee = new Guarantee();

                guarantee.setDormitoryid(resultSet.getString("dormitoryid"));
                guarantee.setStudentname(resultSet.getString("studentname"));
                guarantee.setGoodsname(resultSet.getString("goodsname"));
                guarantee.setReason(resultSet.getString("reason"));
                guarantee.setGuaranteetime(resultSet.getDate("guaranteetime"));
                guarantee.setPhoneid(resultSet.getString("phoneid"));
                guarantee.setGuaranteestaus(resultSet.getString("guaranteestaus"));

                guarantees.add(guarantee);
            }
            if (guarantees == null) {
                return null;
            } else {
                return guarantees;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DriverManegerUitl.closed(conn, pre, resultSet);
        }
        return null;
    }



    //根据宿舍号查询维修信息
    public ArrayList<Guarantee> getGuaranteesByDormitoryid(String dormitoryid) {
        ArrayList<Guarantee> guarantees = new ArrayList<>();
        conn = DriverManegerUitl.getConnection();
        try {
            pre = conn.prepareStatement("select *from guarantee where dormitoryid=?");
            pre.setString(1, dormitoryid);
            resultSet = pre.executeQuery();
            while (resultSet.next()) {

                Guarantee guarantee = new Guarantee();

                guarantee.setDormitoryid(resultSet.getString("dormitoryid"));
                guarantee.setStudentname(resultSet.getString("studentname"));
                guarantee.setGoodsname(resultSet.getString("goodsname"));
                guarantee.setReason(resultSet.getString("reason"));
                guarantee.setGuaranteetime(resultSet.getDate("guaranteetime"));
                guarantee.setPhoneid(resultSet.getString("phoneid"));
                guarantee.setGuaranteestaus(resultSet.getString("guaranteestaus"));

                guarantees.add(guarantee);
            }
            if (guarantees == null) {
                return null;
            } else {
                return guarantees;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DriverManegerUitl.closed(conn, pre, resultSet);
        }
        return null;
    }

    /**
     * 学生提交维护信息
     *
     * @param guarantee
     */
//学生提交修改维修信息
    public void submit(Guarantee guarantee) {
        conn = DriverManegerUitl.getConnection();
        try {
            pre = conn.prepareStatement("insert into guarantee(dormitoryid,studentname,goodsname,reason,phoneid,guaranteestaus)value (?,?,?,?,?,?)");
            pre.setString(1, guarantee.getDormitoryid());
            pre.setString(2, guarantee.getStudentname());
            pre.setString(3, guarantee.getGoodsname());
            pre.setString(4, guarantee.getReason());
            pre.setString(5, guarantee.getPhoneid());
            pre.setString(6, "维修中");

            pre.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DriverManegerUitl.closed(conn, pre, resultSet);
        }
    }

    /**
     * 管理员端
     * 查询表里所有维修的信息
     *
     * @return
     */
    //查询表里所有维修的信息
    public ArrayList<Guarantee> getAllGuarantee() {
        conn = DriverManegerUitl.getConnection();
        ArrayList<Guarantee> list = new ArrayList<>();
        try {
            pre = conn.prepareStatement("select * from guarantee");
            resultSet = pre.executeQuery();
            while (resultSet.next()) {
                Guarantee guarantee = new Guarantee();
                guarantee.setDormitoryid(resultSet.getString("dormitoryid"));
                guarantee.setStudentname(resultSet.getString("studentname"));
                guarantee.setGoodsname(resultSet.getString("goodsname"));
                guarantee.setReason(resultSet.getString("reason"));
                guarantee.setGuaranteetime(resultSet.getDate("guaranteetime"));
                guarantee.setPhoneid(resultSet.getString("phoneid"));
                guarantee.setGuaranteestaus(resultSet.getString("guaranteestaus"));
                list.add(guarantee);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DriverManegerUitl.closed(conn, pre, resultSet);
        }
        return null;
    }

    /**
     * 管理员端
     *修改
     * @param guarantee
     */
    //根据宿舍号，姓名修改报修状态,报修时间
    public void changeGuarangeeStaus(Guarantee guarantee) {
        conn = DriverManegerUitl.getConnection();
        try {
            pre = conn.prepareStatement("update guarantee set guaranteestaus=?,guaranteetime=? where dormitoryid=? and studentname=?");
            pre.setString(1, guarantee.getGuaranteestaus());
            pre.setDate(2, new java.sql.Date(guarantee.getGuaranteetime().getTime()));
            pre.setString(3, guarantee.getDormitoryid());
            pre.setString(4, guarantee.getStudentname());
            pre.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DriverManegerUitl.closed(conn, pre, resultSet);
        }
    }

//根据宿舍号、姓名删除信息
    public void DeleInfoByDormidAndStuname(String dormitoryid,String studentname){
        conn=DriverManegerUitl.getConnection();
        try {
            pre=conn.prepareStatement("delete from guarantee where dormitoryid=? and studentname=? ");
            pre.setString(1,dormitoryid);
            pre.setString(2,studentname);
            pre.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DriverManegerUitl.closed(conn, pre, resultSet);
        }
    }
}
